package com.stylefeng.guns.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;

import static com.stylefeng.guns.core.util.ToolUtil.isEmpty;

/**
 * 读取guns项目配置（application.yml）
 *
 * @author stylefeng
 * @Date 2017/5/23 22:31
 */
@Component
@ConfigurationProperties(prefix = GunsProperties.PREFIX)
public class GunsProperties {

    public static final String PREFIX = "guns";

    public static final String LINUX_PATH_PERFIX = "/root";

    private Boolean kaptchaOpen = false;

    private Boolean swaggerOpen = false;

    //windows目录
    private String fileUploadPathWin;

    //linux目录
    private String fileUploadPathLinux;

    //临时文件目录
    private String tempPath;

    //头像目录
    private String avatarPath;

    //二维码目录
    private String qaCodePath;

    //“茶叶介绍”图片上传路径
    private String teaPath;

    private Boolean haveCreatePath = false;

    private Boolean springSessionOpen = false;

    //session 失效时间（默认为30分钟 单位：秒）
    private Integer sessionInvalidateTime = 30 * 60;

    //session 验证失效时间（默认为15分钟 单位：秒）
    private Integer sessionValidationInterval = 15 * 60;

    /**
     * 获得文件上传根路径
     */
    public String getFileUploadPath() {
        //判断系统的类型来选择目录
        String fileUploadPath;
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            fileUploadPath = fileUploadPathWin;
        } else {
            fileUploadPath = fileUploadPathLinux;
        }

        //如果没有写文件上传路径,保存到临时目录
        if (isEmpty(fileUploadPath)) {
            return getTempPath();
        } else {
            fileUploadPath = checkSeparator(fileUploadPath);
            return fileUploadPath;
        }
    }

    /**
     * 删除前缀下划线，加上后缀下划线
     */
    private String checkSeparator(String resourcePath) {
        if (resourcePath.startsWith(File.separator)) {
            if(!resourcePath.startsWith(LINUX_PATH_PERFIX)){
                resourcePath = resourcePath.substring(1);
            }
        }
        if (!resourcePath.endsWith(File.separator)) {
            resourcePath = resourcePath + File.separator;
        }
        return resourcePath;
    }

    /**
     * 获得临时文件目录
     */
    public String getTempPath() {
        return getFileUploadPath() + checkSeparator(tempPath);
    }

    /**
     * 获得头像上传路径
     */
    public String getAvatarPath() {
        return getFileUploadPath() + checkSeparator(avatarPath);
    }

    /**
     * 获得二维码图片上传路径
     */
    public String getQaCodePath() {
        return getFileUploadPath() + checkSeparator(qaCodePath);
    }

    /**
     * 获得“茶叶介绍”图片上传路径
     */
    public String getTeaPath() {
        return getFileUploadPath() + checkSeparator(teaPath);
    }

    /**
     * get set方法区
     */
    public void setFileUploadPathWin(String fileUploadPathWin) {
        this.fileUploadPathWin = fileUploadPathWin;
    }

    public void setFileUploadPathLinux(String fileUploadPathLinux) {
        this.fileUploadPathLinux = fileUploadPathLinux;
    }

    public void setTempPath(String tempPath) {
        this.tempPath = tempPath;
    }

    public void setAvatarPath(String avatarPath) {
        this.avatarPath = avatarPath;
    }

    public void setQaCodePath(String qaCodePath) {
        this.qaCodePath = qaCodePath;
    }

    public void setTeaPath(String teaPath) {
        this.teaPath = teaPath;
    }

    public Boolean getKaptchaOpen() {
        return kaptchaOpen;
    }

    public void setKaptchaOpen(Boolean kaptchaOpen) {
        this.kaptchaOpen = kaptchaOpen;
    }

    public Boolean getSwaggerOpen() {
        return swaggerOpen;
    }

    public void setSwaggerOpen(Boolean swaggerOpen) {
        this.swaggerOpen = swaggerOpen;
    }

    public Boolean getSpringSessionOpen() {
        return springSessionOpen;
    }

    public void setSpringSessionOpen(Boolean springSessionOpen) {
        this.springSessionOpen = springSessionOpen;
    }

    public Integer getSessionInvalidateTime() {
        return sessionInvalidateTime;
    }

    public void setSessionInvalidateTime(Integer sessionInvalidateTime) {
        this.sessionInvalidateTime = sessionInvalidateTime;
    }

    public Integer getSessionValidationInterval() {
        return sessionValidationInterval;
    }

    public void setSessionValidationInterval(Integer sessionValidationInterval) {
        this.sessionValidationInterval = sessionValidationInterval;
    }
}
